﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;
using LuaInterface;
using System.Lua;
using System.Lua.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
       

        static void Main(string[] args)
        {
            string luafile = "test.lua";
            StringBuilder stringBuilder;
            LuaWriter luaWriter;
            LuaSerializer luaSerializer;

            PropertyTest pt = new PropertyTest();
            pt.TestInt = 1;
            pt.TestFloat = 0.5f;
            pt.TestBool = true;
            pt.Name = "pt";

            PropertyTest2 innerP = new PropertyTest2();
            innerP.Name = "innerP";
            innerP.TestFloat = 0.885f;
            innerP.TestEnum = PropertyTest.TestEnumType.Value3;
            // innerP.Color = Color.Yellow;
            //innerP.Position = new Vector2(48,653.5f);

            PropertyTest2 root = new PropertyTest2();
            root.TestInt = 2;
            root.TestFloat = 0.8f;
            root.TestBool = false;
            root.Name = "pt2";
            root.Color = Color.Khaki;
            root.Child = innerP;
            root.Position = new Vector2(1.5f, 6f);
            root.ps = new List<PropertyTest>();
            root.ps.Add(innerP);

            root.pd.Add("t0", pt);
            root.pd.Add("t1", new PropertyTest2());



            stringBuilder = new StringBuilder();
            luaWriter = LuaWriter.Create(stringBuilder);
            luaSerializer = new LuaSerializer(typeof(PropertyTest2));

            luaSerializer.Serialize(luaWriter, root);
            //Console.WriteLine(stringBuilder);
            StreamWriter file = new System.IO.StreamWriter(luafile);
            file.Write(stringBuilder);
            file.Close();

            PropertyTest2 deserP = (PropertyTest2)luaSerializer.Deserialize(luafile);


            //
            //writeProperty(deserP);
            //writeProperty(root);
            //
            //Console.WriteLine(deserP.Color);

            writeProperty(root.Child);
            writeProperty(deserP.Child);



            Console.Write("\r\nPress any key to continue....");
            Console.ReadKey();
        }


        private static void writeTable(string name, LuaTable table)
        {
            Console.WriteLine(name + "={");
            foreach (var key in table.Keys)
            {
                if (table[key].GetType() == typeof(LuaTable))
                {
                    writeTable(key.ToString(), (LuaTable)table[key]);
                }
                else
                {
                    Console.WriteLine(key.ToString() + "=" + table[key].ToString());
                }
            }
            Console.WriteLine("}");
        }

        private static void writeProperty(object o)
        {
            Console.WriteLine(o.ToString());
            PropertyInfo[] pi = o.GetType().GetProperties();
            foreach (PropertyInfo p in pi)
            {
                Console.WriteLine(p.PropertyType + " " + p.Name + ":" + p.GetValue(o, null));
            }
        }


    }
}
